//
//  DDInvoiceManagerInfoVC.swift
//  DingDongZP
//
//  Created by ZZN on 16/10/23.
//  Copyright © 2016年 51ledian.com. All rights reserved.
//

import UIKit

class DDInvoiceManagerInfoVC: LDBaseTableViewVC {
    
    // MARK: - Table view data source
    private let cellDetailID = "DDChoiceInvoiceInfoVCDetailID"
    
    var model:DDInvoiceInfoModel?
    private var type = DDInvoiceSettingInfoVCType.Edit
    
    override func viewDidLoad() {
        super.viewDidLoad()
        tableView.separatorStyle = .None
    }
    
    override func viewWillAppear(animated: Bool) {
        
        requsetNet()
    }
    

    func requsetNet() {
        
        LDUserInfoNet.sharedInstance.getInvoiceBillInfoList({[weak self] (dictArr) in
            
            self?.model = DDInvoiceInfoModel.yy_modelWithDictionary(dictArr)
//            print(self?.model?.list.count)
            self?.tableView.reloadData()
            
        }) { (err) in
            
            
        }
    }
    
    
    override func setNav() {
        
        let rightBar = UIBarButtonItem.init(title: "管理", style: .Done, target: self, action: #selector(tapRightBar))
        navigationItem.rightBarButtonItem = rightBar
        
    }
    
    //right bar
    override func tapRightBar() {
        
        let vc = DDInvoiceSettingInfoVC()
        vc.title = "发票信息设置"
        navigationController?.pushViewController(vc, animated: true)
    }
    
    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        
        sendParams(indexPath)
    }
    
    //设置默认 抬头
    func sendParams(indexPath: NSIndexPath) {
        
        let listModel = model?.list[indexPath.row] ?? DDInvoiceInfoModelList() //as? DDInvoiceInfoModelList ?? DDInvoiceInfoModelList()
        listModel.state = 1
        

   
        let dict = listModel.yy_modelToJSONObject() as? NSDictionary ?? NSDictionary()
        LDUserInfoNet.sharedInstance.editOrAddInvoiceBillInfo(nil, params: dict as? Dictionary<String, AnyObject>, succ: { [weak self] (str) in
            
            
            LDShowHUB.shareInstance.showSucc("切换默认地址成功")
            self?.navigationController?.popViewControllerAnimated(true)
            
        }) { (err) in
            
            LDShowHUB.shareInstance.showErr("切换默认地址成功")

        }
    }
    
}

extension DDInvoiceManagerInfoVC {
    
    
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return model?.list.count ?? 0
    }
    
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
        
        var cell = tableView.dequeueReusableCellWithIdentifier(cellDetailID) as? DDInvoiceDetailViewCell
        if cell == nil {
            
            cell = DDInvoiceDetailViewCell(topMargin:LDFrame.TableCellMargin,style: .Default, reuseIdentifier: cellDetailID)
        }
        
        let listModel = model?.list[indexPath.row]// as? DDInvoiceInfoModelList
        
        cell?.descLabArr = [listModel!.name,listModel!.link_name,listModel!.link_mobile,listModel!.province,listModel!.address]
        cell?.status = listModel?.state == 1 ? true:false
        
        return cell!
        
    }
    
    override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        
        return 270
    }
}
